package com.zhixinhuixue.zsyte.xxx.ui.activity

import android.os.Bundle
import android.text.TextUtils
import android.widget.CompoundButton
import androidx.lifecycle.Observer
import com.zhixinhuixue.zsyte.xxx.R
import com.zhixinhuixue.zsyte.xxx.app.App
import com.zhixinhuixue.zsyte.xxx.app.api.NetUrl
import com.zhixinhuixue.zsyte.xxx.app.base.BaseActivity
import com.zhixinhuixue.zsyte.xxx.app.ext.initBack
import com.zhixinhuixue.zsyte.xxx.data.annotation.ValueKey
import com.zhixinhuixue.zsyte.xxx.databinding.ActivityConfigurationBinding
import com.zhixinhuixue.zsyte.xxx.ui.viewmodel.PasswordViewModel
import me.hgj.mvvmhelper.ext.getStringExt
import me.hgj.mvvmhelper.ext.showDialogMessage
import me.hgj.mvvmhelper.net.LoadStatusEntity
import me.hgj.mvvmhelper.util.SPUtil
import rxhttp.RxHttpPlugins

/**
 * 作者　: hegaojian
 * 时间　: 2020/11/18
 * 描述　: 虽然在Activity代码少了，但是DataBinding 不太好用
 */
class ConfigurationActivity : BaseActivity<PasswordViewModel, ActivityConfigurationBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        //初始化toolbar
        mToolbar.initBack("配置信息") {
            finish()
        }
        mDataBind.viewModel = mViewModel
        mDataBind.click = LoginClickProxy()

//        var client_id: String =
//            SPUtil.get(baseContext, ValueKey.CLIENT_ID_KEY, NetUrl.CLIENT_ID) as String
//        var client_secret =
//            SPUtil.get(baseContext, ValueKey.CLIENT_SECRET_KEY, NetUrl.CLIENT_SECRET) as String


        var host = SPUtil.get(baseContext, ValueKey.HOST_KEY, NetUrl.DEV_URL) as String

        var logo = SPUtil.get(baseContext, ValueKey.LOGO_KEY, NetUrl.LOGO) as String


        mDataBind.edtHost.setText(if (TextUtils.isEmpty(host)) "" else host)
        mDataBind.edtLogo.setText(if (TextUtils.isEmpty(logo)) "" else logo)

    }

    /**
     * 请求成功
     */
    override fun onRequestSuccess() {
        //监听登录结果
        mViewModel.passwordData.observe(this, Observer {
            App.instance.restartApplication()
        })
    }

    /**
     * 请求失败
     * @param loadStatus LoadStatusEntity
     */
    override fun onRequestError(loadStatus: LoadStatusEntity) {
        when (loadStatus.requestCode) {
            NetUrl.PASSWORD -> {
                showDialogMessage(loadStatus.errorMessage)
            }
        }
    }

    inner class LoginClickProxy {

        fun clear() {
            mViewModel.userName.set("")
        }

        //登录
        fun login() {
            var local: String =
                SPUtil.get(this@ConfigurationActivity, SPUtil.PASSWORD, "") as String
            var input: String = mViewModel.oldPassword.get()

            when {
                mDataBind.edtLogo.text.toString().isNullOrEmpty() -> showDialogMessage("Logo")
                mDataBind.edtHost.text.toString().isNullOrEmpty() -> showDialogMessage("主机头不能为空")
                else -> {
                    SPUtil.put(baseContext, ValueKey.HOST_KEY, mDataBind.edtHost.text.toString())
//                    SPUtil.put(baseContext, ValueKey.CLIENT_ID_KEY, mDataBind.edtId.text.toString())
//                    SPUtil.put(baseContext, ValueKey.CLIENT_SECRET_KEY, mDataBind.edtSecret.text.toString())
//                    SPUtil.put(baseContext, ValueKey.LOGO_KEY, mDataBind.edtLogo.text.toString())
//                    SPUtil.put(baseContext, ValueKey.isFirst, false)

                    App.instance.restartApplication()
                }
            }
        }

        var onOldCheckedChangeListener =
            CompoundButton.OnCheckedChangeListener { buttonView, isChecked ->
                mViewModel.isShowOldPwd.set(isChecked)
            }
        var onNewCheckedChangeListener =
            CompoundButton.OnCheckedChangeListener { buttonView, isChecked ->
                mViewModel.isShowNewPwd.set(isChecked)
            }
        var onReNewCheckedChangeListener =
            CompoundButton.OnCheckedChangeListener { buttonView, isChecked ->
                mViewModel.isShowReNewPwd.set(isChecked)
            }
    }

//    override fun showCustomLoading(setting: LoadingDialogEntity) {
//        if(setting.requestCode== NetUrl.LOGIN){
//            //可以根据不同的code 做不同的loading处理
//            showLoadingUi()
//        }
//    }
//
//    override fun dismissCustomLoading(setting: LoadingDialogEntity) {
//        if(setting.requestCode==NetUrl.LOGIN){
//            //可以根据不同的code 做不同的loading处理
//            showSuccessUi()
//        }
//    }

}